package com.anhry.www.safety.manage.zczb.struts.action;

import java.io.File;
import java.util.Calendar;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.anhry.www.safety.manage.enterprise.bean.TbCorpInfo;
import com.anhry.www.safety.manage.zczb.bean.TbZczbYh;
import com.anhry.www.safety.manage.zczb.bean.TbZczbYhFj;
import com.anhry.www.safety.manage.zczb.struts.base.ZczbBaseAction;
import com.anhry.www.safety.util.DateFormater;
import com.anhry.www.safety.util.Loggers;
import com.anhry.www.safety.util.UploadFile;
import com.anhry.www.safety.util.Util;
import com.anhry.www.safety.util.UtilMethod;

/**
 * @date:2012-10-19
 * @remark:未处理隐患管理
 * @author:dongbin
 */

@Controller
@Scope("prototype")
@SuppressWarnings("unchecked")
public class WclAction extends ZczbBaseAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String yhIsZg;//隐患是否整改0：未整改1：已整改
	private String yhWcTime;//整改完成日期
	private String yhGzZrr;//整改责任人
	private String yhZrrTel;//整改责任人电话
	private String yhZgQk;//隐患整改情况
	
	/**
	 * 报告单列表，列出本年度所有上报列表
	 * @return
	 */
	public String list(){
		try {
			TbCorpInfo corp = (TbCorpInfo)this.contextPvd.getSessionAttr(Util.SESSION_CORPUSER);
			Calendar cal = Calendar.getInstance();
			int year = cal.get(Calendar.YEAR);
			//StringBuilder hql = new StringBuilder("from TbZczbHzd t where 1=1 and t.hzdWclNum>0 and t.hzdStatus=1 and t.corpId="+corp.getId());
			StringBuilder hql = new StringBuilder("from TbZczbHzd t where 1=1 and t.hzdWclNum>0 and t.corpId="+corp.getId());
			if(null!=this.cYear && this.cYear.length()>0){
				hql.append(" and t.hzdYear="+this.cYear);
			}else{
				hql.append(" and t.hzdYear="+year);
				this.setCYear(String.valueOf(year));
			}
			hql.append("order by t.hzdMonth");
			this.setObjList((List)this.utilService.getHqlQuery(hql.toString()));
			this.setYearList(UtilMethod.getYearList(2012));
			this.setResult("wcl_hzd_list");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("WclAction list() 异常："+e.getMessage());
		}
		return ERROR;
	}
	
	/**
	 * 隐患列表
	 * @return
	 */
	public String yhList(){
		try {
			if(null!=this.hzdId && this.hzdId.length()>0){
				this.setObjList(this.zczbService.getYhList(null, this.hzdId, null, null, this.cType1, "0"));
				this.setResult("wcl_yh_list");
				return SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("WclAction yhList() 异常："+e.getMessage());
		}
		this.setTip("数据不存在！");
		return ERROR;
	}
	
	/**
	 * 隐患整改
	 * @return
	 */
	public String yhZg(){
		try {
			if(null!=this.yhId && this.yhId.length()>0){
				TbZczbYh yh = (TbZczbYh)this.utilService.getObjectById(TbZczbYh.class, Long.parseLong(this.yhId));
				if(null!=yh){
					this.setYh(yh);
					this.setFjList1(this.zczbService.getYhFjList("1",this.yhId));
					this.setFjList2(this.zczbService.getYhFjList("2",this.yhId));
					this.setFjList3(this.zczbService.getYhFjList("3",this.yhId));
					this.setResult("wcl_yh_zg");
					return SUCCESS;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("WclAction yhZg() 异常："+e.getMessage());
		}
		this.setTip("您要操作的数据不存在！");
		return ERROR;
	}
	
	/**
	 * 隐患保存
	 * @return
	 */
	public String saveYh(){
		try {
			if(null!=this.yhId && this.yhId.length()>0){
				TbZczbYh yh = (TbZczbYh)this.utilService.getObjectById(TbZczbYh.class, Long.parseLong(this.yhId));
				if(null!=yh){
					if(null!=this.yhWcTime && this.yhWcTime.length()>0){
						yh.setYhWcTime(DateFormater.formatDate(yhWcTime));
					}
					yh.setYhZgZrr(this.yhGzZrr);
					yh.setYhZrrTel(this.yhZrrTel);
					yh.setYhIsZg(this.yhIsZg);
					yh.setYhZgQk(this.yhZgQk);
					this.utilService.update(yh);
					this.zczbService.initBgd(yh.getBgdId()+"");
					this.setTip("保存成功！");
					return this.saveYhfj(yh);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("WclAction saveYh() 异常："+e.getMessage());
		}
		this.setTip("您要操作的数据不存在！");
		return ERROR;
	}
	
	/**
	 * 隐患附件保存
	 * @param yh
	 */
	public String saveYhfj(TbZczbYh yh){
		try {
			if(null!=this.picFile && this.picFile.length>0){
				for(int i=0;i<this.picFile.length;i++){
					if(!UtilMethod.filterType(this.picFileContentType[i], Util.ALLOWTYPES.split(","))){
						this.setTip("图片附件格式错误,请重新上传！");
						return this.yhZg();
					}
				}
				for(int i=0;i<this.picFile.length;i++){
					TbZczbYhFj fj = new TbZczbYhFj();
					fj.setFjTitle(this.picFileFileName[i]);
					String tem_path = Util.SAFETY_FILE+File.separator+Util.FILE_FOLDER+File.separator+Util.ZCZB;
					String fileName = UploadFile.copy2(this.picFile[i], tem_path, this.picFileFileName[i]);
					String path = Util.FILE_FOLDER+File.separator+Util.ZCZB+File.separator+fileName;
					fj.setFjPath(path);
					fj.setIsDele("0");
					fj.setFjType("2");
					fj.setYhId(yh.getId());
					this.utilService.save(fj);
				}
			}
			if(null!=this.docFile && this.docFile.length>0){
				for(int i=0;i<this.docFile.length;i++){
					TbZczbYhFj fj = new TbZczbYhFj();
					fj.setFjTitle(this.docFileFileName[i]);
					String tem_path = Util.SAFETY_FILE+File.separator+Util.FILE_FOLDER+File.separator+Util.ZCZB;
					String fileName = UploadFile.copy2(this.docFile[i], tem_path, this.docFileFileName[i]);
					String path = Util.FILE_FOLDER+File.separator+Util.ZCZB+File.separator+fileName;
					fj.setFjPath(path);
					fj.setIsDele("0");
					fj.setFjType("3");
					fj.setYhId(yh.getId());
					this.utilService.save(fj);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczbAction saveYhfj(TbZczbYh yh) 异常："+e.getMessage());
		}
		this.setTip("操作成功！");
		return this.yhList();
	}

	public String getYhGzZrr() {
		return yhGzZrr;
	}

	public void setYhGzZrr(String yhGzZrr) {
		this.yhGzZrr = yhGzZrr;
	}

	public String getYhIsZg() {
		return yhIsZg;
	}

	public void setYhIsZg(String yhIsZg) {
		this.yhIsZg = yhIsZg;
	}

	public String getYhWcTime() {
		return yhWcTime;
	}

	public void setYhWcTime(String yhWcTime) {
		this.yhWcTime = yhWcTime;
	}

	public String getYhZrrTel() {
		return yhZrrTel;
	}

	public void setYhZrrTel(String yhZrrTel) {
		this.yhZrrTel = yhZrrTel;
	}

	public String getYhZgQk() {
		return yhZgQk;
	}

	public void setYhZgQk(String yhZgQk) {
		this.yhZgQk = yhZgQk;
	}

}
